import logging
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from edge import driver
import get_boss_filterByLabel


def login():
    # 如果二维码过期，点击刷新，直到检测到用户已登录
    while True:
        try:
            # 判断是否登录成功，检查某些登录后的页面元素是否出现
            WebDriverWait(driver, 3).until(
                EC.presence_of_element_located((By.XPATH, '//*[@id="wrap"]/div[1]/div[1]/dl[1]/dt/a'))
            )
            logging.info("登录成功！")

            get_boss_filterByLabel.get_boss_filterByLabel(driver)  # 获取friendId和friendSource进行保存

            # 判断是否有下载弹框，有就点击关闭，没有则跳过
            try:
                # 等待弹框关闭按钮的出现
                close_button = WebDriverWait(driver, 3).until(
                    EC.presence_of_element_located((By.CLASS_NAME, 'boss-popup__close'))
                )

                # 点击关闭按钮
                # 创建 ActionChains 对象
                actions = ActionChains(driver)
                # 将鼠标移动到目标元素并点击
                actions.move_to_element(close_button).click().perform()
                # close_button.click()
                logging.info("已关闭弹框。")

            except Exception as e:
                logging.info("未找到弹框。")
            break

        except TimeoutException:

            try:
                # 等待刷新按钮可见
                refresh_btn = WebDriverWait(driver, 3).until(
                    EC.presence_of_element_located(
                        (By.XPATH, '//*[@id="wrap"]/div/div[2]/div[2]/div[2]/div[2]/div[1]/div/button'))
                )
                # 如果刷新按钮存在，点击刷新按钮
                # refresh_btn.click()
                # 创建 ActionChains 对象
                actions = ActionChains(driver)
                # 将鼠标移动到目标元素并点击
                actions.move_to_element(refresh_btn).click().perform()
                logging.info("二维码已过期，已刷新新的二维码...")

            except TimeoutException:
                # 如果刷新按钮没有找到，进入app扫码登录的逻辑
                try:
                    if driver.find_element(By.XPATH,
                                           '//*[@id="wrap"]/div/div[2]/div[2]/div[2]/h3'):
                        resumes_xpaths = driver.find_element(By.XPATH,
                                                             '//*[@id="wrap"]/div/div[2]/div[2]/div[2]/h3')
                        # 获取该元素的文本
                        elements_text = resumes_xpaths.text.strip()  # .strip() 去除多余的空格或换行符
                        # 判断文本
                        if elements_text == "验证码登录/注册":
                            app = driver.find_element(By.XPATH, '//*[@id="wrap"]/div/div[2]/div[2]/div[1]')
                            # app.click()
                            # 创建 ActionChains 对象
                            actions = ActionChains(driver)
                            # 将鼠标移动到目标元素并点击
                            actions.move_to_element(app).click().perform()
                            logging.info("切换app扫码登录...")
                except Exception as e:
                    logging.info("请扫码登录...")
                    continue
